package cz.muni.fi.pa165.machineryrental.rest;

import cz.muni.fi.pa165.machineryrental.dto.MachineDTO;
import cz.muni.fi.pa165.machineryrental.service.MachineService;
import java.util.List;
import javassist.tools.rmi.ObjectNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 *
 * @author Marti_000
 */
@Controller
@RequestMapping(value = "/machineRest")
public class MachineRestController {

    @Autowired
    private MachineService machineService;

    @RequestMapping(value = "/getById/{id}", method = RequestMethod.GET)
    @ResponseBody
    public MachineDTO getById(@PathVariable(value = "id") long id) {
        return machineService.getById(id);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(value = HttpStatus.CREATED)
    public void create(@RequestBody MachineDTO machine) {
        machineService.create(machine);
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(value = HttpStatus.ACCEPTED)
    public void update(@PathVariable(value = "id") long id, @RequestBody MachineDTO machine) {
        MachineDTO machineDTO = machineService.getById(id);
        machineDTO.setBrand(machine.getBrand());
        machineDTO.setCode(machine.getCode());
        machineDTO.setModel(machine.getModel());
        machineDTO.setYearOfProduction(machine.getYearOfProduction());
        machineService.update(machineDTO);
    }

    @RequestMapping(value = "/remove/{id}", method = RequestMethod.DELETE)
    @ResponseStatus(value = HttpStatus.NO_CONTENT)
    public void remove(@PathVariable("id") long id) throws ObjectNotFoundException {
        MachineDTO machine = machineService.getById(id);
        if (machine == null) {
            throw new ObjectNotFoundException("Machine not found");
        }

        machineService.remove(machine);
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public List<MachineDTO> findAll() {
        return machineService.findAll();
    }

    @RequestMapping(value = "/findByCode/{code}", method = RequestMethod.GET)
    @ResponseBody
    public MachineDTO findByCode(@PathVariable(value = "code") String code) {
        return machineService.findByCode(code);
    }

    @RequestMapping(value = "/findByBrand/{brand}", method = RequestMethod.GET)
    @ResponseBody
    public List<MachineDTO> findByBrand(@PathVariable(value = "brand") String brand) {
        return machineService.findByBrand(brand);
    }

    @RequestMapping(value = "/findByModel/{model}", method = RequestMethod.GET)
    @ResponseBody
    public List<MachineDTO> findByModel(@PathVariable(value = "model") String model) {
        return machineService.findByModel(model);
    }

    @RequestMapping(value = "/isCodeUnique/{code}", method = RequestMethod.GET)
    @ResponseBody
    public boolean isCodeUnique(@PathVariable(value = "code") String code) {
        return this.machineService.isCodeUnique(code);
    }
}
